﻿# -*- coding: utf-8 -*-
import numpy as np
from scipy.signal import convolve2d
from PIL import Image, ImageDraw

img = Image.open('lenna.jpg')
gray = img.convert('L')
gray.save('gray.jpg')

k1 = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]], np.float32)
img1 = convolve2d(gray, k1, 'same')
Image.fromarray(img1.astype('uint8')).save('img1.jpg')

k2 = np.array([[0,1,0],[1,0,1],[0,1,0]],np.float32) /4.
img2 = convolve2d(gray, k2, 'same')
Image.fromarray(img2.astype('uint8')).save('img2.jpg')

k3 = np.array([[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,7,4,1]],np.float32) /273.
img3 = convolve2d(gray, k3, 'same')
Image.fromarray(img3.astype('uint8')).save('img3.jpg')

k4 = np.array([[-1,1,0],[-1,0,1],[0,1,1]],np.float32)
img4 = convolve2d(gray, k4, 'same')
Image.fromarray(img4.astype('uint8')).save('img4.jpg')